package cn.hdu.goods.book.servlet;

import cn.hdu.goods.book.domain.Book;
import cn.hdu.goods.book.service.BookService;
import cn.hdu.goods.pager.PageBean;
import cn.itcast.commons.CommonUtils;
import cn.itcast.servlet.BaseServlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;

@WebServlet("/BookServlet")
public class BookServlet extends BaseServlet {
    private BookService bookService = new BookService();

    public String load(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String bid = req.getParameter("bid");
        Book book = bookService.load(bid);
        req.setAttribute("book", book);
        return "f:/jsps/book/desc.jsp";
    }


    public String findByCategory(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int pc = getPc(req);
        String url = getUrl(req);
        String cid = req.getParameter("cid");
        PageBean<Book> pb = bookService.findByCategory(cid, pc);
        pb.setUrl(url);
        req.setAttribute("pb", pb);
        return "f:/jsps/book/list.jsp";
    }

    public String findByAuthor(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        int pc = getPc(req);
        String url = getUrl(req);
        String author = getChinessCriteria(req, "author");
        PageBean<Book> pb = bookService.findByAuthor(author, pc);
        pb.setUrl(url);
        req.setAttribute("pb", pb);
        return "f:/jsps/book/list.jsp";
    }

    public String findByPress(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int pc = getPc(req);
        String url = getUrl(req);
        String press = getChinessCriteria(req, "press");

        PageBean<Book> pb = bookService.findByPress(press, pc);
        pb.setUrl(url);
        req.setAttribute("pb", pb);
        return "f:/jsps/book/list.jsp";
    }

    public String findByBname(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int pc = getPc(req);
        String url = getUrl(req);
        String bname = getChinessCriteria(req, "bname");
        PageBean<Book> pb = bookService.findByBname(bname, pc);
        pb.setUrl(url);
        req.setAttribute("pb", pb);
        return "f:/jsps/book/list.jsp";
    }

    public String findByCombination(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int pc = getPc(req);
        String url = getUrl(req);
        Book criteria = CommonUtils.toBean(getChineseCombination(req), Book.class);
        PageBean<Book> pb = bookService.findByCombination(criteria, pc);
        pb.setUrl(url);
        req.setAttribute("pb", pb);
        return "f:/jsps/book/list.jsp";
    }


    private Map<String, String> getChineseCombination(HttpServletRequest req) {
        Map<String, String> map = new HashMap<>();

        String[] reqUrls = req.getQueryString().split("&");
        for (String reqUrl : reqUrls) {
            if (!reqUrl.contains("method")) {
                try {
                    String value = URLDecoder.decode(reqUrl.substring(reqUrl.lastIndexOf("=") + 1), "UTF-8");
                    map.put(reqUrl.substring(0, reqUrl.lastIndexOf("=")), value);
                } catch (UnsupportedEncodingException e) {
                    return null;
                }
            }
        }
        return map;
    }

    private int getPc(HttpServletRequest req) {
        int pc = 1;
        String param = req.getParameter("pc");
        if (param != null && !param.trim().isEmpty()) {
            try {
                pc = Integer.parseInt(param);
            } catch (RuntimeException ignored) {

            }
        }
        return pc;
    }

    private String getUrl(HttpServletRequest req) {
        String url = req.getRequestURI() + "?" + req.getQueryString();
        int index = url.lastIndexOf("&pc=");
        if (index != -1) {
            url = url.substring(0, index);
        }
        return url;
    }

    private String getChinessCriteria(HttpServletRequest req, String criteria) {
        String[] reqUrls = req.getQueryString().split("&");
        for (String reqUrl : reqUrls) {
            if (reqUrl.contains(criteria)) {
                try {
                    return URLDecoder.decode(reqUrl.substring(reqUrl.lastIndexOf("=") + 1), "UTF-8");
                } catch (UnsupportedEncodingException e) {
                    return null;
                }
            }

        }
        return null;
    }

}
